package com.gmail.fanvaron.Listener;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Dispenser;
import org.bukkit.block.Dropper;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;

import com.gmail.fanvaron.Main.MarvMonsterPack;
import com.gmail.fanvaron.Utils.Utils;

public class DispenseListener implements Listener {

    public MarvMonsterPack plugin;

    public DispenseListener(MarvMonsterPack instance) {
        plugin = instance;
    }

    @SuppressWarnings({ "deprecation" })
    @EventHandler
    public void onDispense(BlockDispenseEvent event) {

        Block block = event.getBlock();
        if (Utils.hasMatchingSign(block, Utils.INFINITE) || block.getWorld().getName().equals("adventure")) {

            ItemStack justDispensed = event.getItem();
            System.out.println(justDispensed.getAmount());

            Inventory dispenserInv = null;
            if (block.getState().getType().equals(Material.DISPENSER)) {
                dispenserInv = ((Dispenser) block.getState()).getInventory();
            } else {
                dispenserInv = ((Dropper) block.getState()).getInventory();
            }
            if (justDispensed.getType().isBlock() && !justDispensed.getType().equals(Material.TNT)
                    && block.getState().getType().equals(Material.DISPENSER)) {
                MaterialData d = block.getState().getData();
                org.bukkit.material.Dispenser disp = (org.bukkit.material.Dispenser) d;
                BlockFace face = disp.getFacing();

                Location loc = block.getLocation();

                loc.setX(loc.getX() + face.getModX());
                loc.setY(loc.getY() + face.getModY());
                loc.setZ(loc.getZ() + face.getModZ());

                Block b = loc.getWorld().getBlockAt(loc);
                b.setTypeIdAndData(justDispensed.getTypeId(), justDispensed.getData().getData(), false);
                /*
                 * if (!BlockUtils.hasMatchingSign(block, BlockUtils.INFINITE))
                 * { dispenserInv.removeItem(justDispensed); int slot =
                 * dispenserInv.first(justDispensed); System.out.println(slot);
                 * ItemStack slotStack = dispenserInv.getItem(slot); if
                 * (slotStack.getAmount() > 1) {
                 * slotStack.setAmount(slotStack.getAmount() - 1);
                 * dispenserInv.setItem(slot, slotStack); } else {
                 * dispenserInv.clear(slot); }
                 * 
                 * }
                 */
                event.setCancelled(true);
                /*
                 * if (BlockUtils.hasMatchingSign(block,
                 * BlockUtils.INFINITE))
                 */
            } else {
                justDispensed.setAmount(1);
                dispenserInv.addItem(justDispensed);
            }

        }
    }

}